from graia.ariadne import Ariadne

from admin.common import CommandSource
from app import instance


async def on_global_friend_enable(app: Ariadne, source: CommandSource):
    cfg = instance.database.account(app.account).g
    if cfg.friend_enabled:
        await source.reply(app, "已经开启了好友消息")
        return

    cfg.friend_enabled = True
    cfg.save()
    await source.reply(app, "开启好友消息成功")


async def on_global_friend_disable(app: Ariadne, source: CommandSource):
    cfg = instance.database.account(app.account).g
    if not cfg.friend_enabled:
        await source.reply(app, "已经关闭了好友消息")
        return

    cfg.friend_enabled = False
    cfg.save()
    await source.reply(app, "关闭好友消息成功")


async def on_global_group_enable(app: Ariadne, source: CommandSource):
    cfg = instance.database.account(app.account).g
    if cfg.group_enabled:
        await source.reply(app, "已经开启了群组消息")
        return

    cfg.group_enabled = True
    cfg.save()
    await source.reply(app, "开启群组消息成功")


async def on_global_group_disable(app: Ariadne, source: CommandSource):
    cfg = instance.database.account(app.account).g
    if not cfg.group_enabled:
        await source.reply(app, "已经关闭了群组消息")
        return

    cfg.group_enabled = False
    cfg.save()
    await source.reply(app, "关闭群组消息成功")


async def on_global_status(app: Ariadne, source: CommandSource):
    cfg = instance.database.account(app.account).g
    await source.reply(
        app,
        f"【状态】\n"
        f"好友消息: {'开启' if cfg.friend_enabled else '关闭'}\n"
        f"群组消息: {'开启' if cfg.group_enabled else '关闭'}")
